var express = require("express");
var router = express.Router();
var multiparty = require("multiparty");
var { YijianModel, UserModel, ListModel } = require("../model/model");
/* GET users listing. */
router.get("/", function (req, res, next) {
  res.send("respond with a resource");
});
// 列表
router.get("/userlist", async (req, res) => {
  let id = req.query.id;
  let list = await ListModel.find({ _id: id });
  res.send(list);
});
// 添加
router.post("/add", async (req, res) => {
  let data = await YijianModel.create(req.body);
  res.send({
    code: 200,
    msg: "成功",
    data,
  });
});
// 意见反馈
// 图片上传
router.post("/upload", async (req, res) => {
  let form = new multiparty.Form();
  form.uploadDir = "upload";
  form.parse(req, (err, fields, files) => {
    console.log(files);
    res.send({ path: "http://8.130.46.189:3000/" + files.file[0].path });
  });
});

// 修改
router.post("/update", async (req, res, next) => {
  //   let id = req.params._id;
  let { ids } = req.body;
  console.log(ids);
  let data = await ListModel.updateOne({ _id: ids[0]._id }, ids[0]);
  res.send(data);
});

router.post("/photeImg", async (req, res) => {
  console.log(req.body, "111111");
  await ListModel.updateOne(
    { _id: req.body.id },
    { $set: { img: req.body.img } }
  );

  res.send({ code: 200, msg: "修改成功" });
});
module.exports = router;
